﻿//  Created by Mathew Purchase.
//  Copyright (c) 2014 Mathew Purchase. All rights reserved.

using UnityEngine;
using System.Collections;

public class Character_NPC : MonoBehaviour 
{

		//---------------------------------------------------------------------------------------------
	// Properties
	
	public int thirst {get; set;}
	public int maxThirst;
	public int hunger {get; set;}
	public int maxHunger;
	public int health {get; set;}
	public int maxHealth;
	public int energy {get; set;}
	public int maxEnergy;
	
	public int NPC_ClassID;
	
	public string NPC_Class;
	
	
	
	//---------------------------------------------------------------------------------------------
	// Members
	
	public Inventory m_npcInventory;
	public settlementPiece m_homeTown;
	public helpRandomName m_nameService;
	public Inventory m_homeTownStore;
	public ItemService m_itemService;
	
	//---------------------------------------------------------------------------------------------
	// Events and Delegates
	
	
	//---------------------------------------------------------------------------------------------
	// Init
	
	void OnEnable () 
	{
		m_npcInventory = GetComponent<Inventory>();
		m_itemService = GameObject.Find ("ItemService").GetComponent<ItemService>();
		m_nameService = GameObject.Find("NameService").GetComponent<helpRandomName>();
			
		
		
		TurnService.DayEnd += HungerThirstUpdate;
		TurnService.DayEnd += ProduceItem;
		TurnService.DayEnd += ConsumeItem;
	}
	
	//---------------------------------------------------------------------------------------------
	
	void OnDisable()
	{
		TurnService.DayEnd -= HungerThirstUpdate;
		TurnService.DayEnd -= ProduceItem;
		TurnService.DayEnd -= ConsumeItem;
	
	}
	
	//---------------------------------------------------------------------------------------------
	public void NPCInit()
	{
	
		hunger = 5;
		thirst = 3;
		energy = 5;
		health = 10;
		DetermineClass();
		name = NPC_Class;
		ProduceItem();
		
	}
	
	//---------------------------------------------------------------------------------------------
	// Core Functions

	void DetermineClass () 
	{
		
		NPC_Class = m_nameService.npc_classes[NPC_ClassID];
	}
	
	//---------------------------------------------------------------------------------------------
	void ProduceItem () 
	{
		switch (NPC_ClassID)
		{
			
			case 1:
			{
				// crofter
				// vegetables
		
				m_homeTownStore.addToInventory(m_itemService.itemList[0]);
				m_homeTownStore.addToInventory(m_itemService.itemList[0]);
				m_homeTownStore.addToInventory(m_itemService.itemList[0]);
				m_homeTownStore.addToInventory(m_itemService.itemList[0]);
				
				// flax
				m_homeTownStore.addToInventory(m_itemService.itemList[2]);
				//cotton
				m_homeTownStore.addToInventory(m_itemService.itemList[3]);
				break;
			}
			case 2:
			{
				// Herdsman
				
				// poultry
				m_homeTownStore.addToInventory(m_itemService.itemList[4]);
				m_homeTownStore.addToInventory(m_itemService.itemList[4]);
				
				// meat
				m_homeTownStore.addToInventory(m_itemService.itemList[5]);
				
				// skins
				m_homeTownStore.addToInventory(m_itemService.itemList[9]);	
				break;
			}
			case 3:
			{
				// Watermaster
				// water
				m_homeTownStore.addToInventory(m_itemService.itemList[1]);
				m_homeTownStore.addToInventory(m_itemService.itemList[1]);
				m_homeTownStore.addToInventory(m_itemService.itemList[1]);
				m_homeTownStore.addToInventory(m_itemService.itemList[1]);
				break;
			}
			case 4:
			{
				// Miner
				
				// Bronze
				m_homeTownStore.addToInventory(m_itemService.itemList[6]);
				// Iron
				m_homeTownStore.addToInventory(m_itemService.itemList[7]);
				break;
			}
			case 5:
			{
				// Smith 
				// need to check if the smith has relevant items in his inventory before producing secondary items. 
				
				// tools
				m_homeTownStore.addToInventory(m_itemService.itemList[9]);
				// armour
				m_homeTownStore.addToInventory(m_itemService.itemList[10]);
				// weapons
				m_homeTownStore.addToInventory(m_itemService.itemList[11]);
				break;
			}
			case 6:
			{
				// Clothier
				// as the smith.
				
				// clothes
				m_homeTownStore.addToInventory(m_itemService.itemList[12]);
				// canvas
				m_homeTownStore.addToInventory(m_itemService.itemList[13]);
				
				
				
				break;
			}
			case 7:
			{
				
				break;
			}
			case 8:
			{
				
				break;
			}
			case 9:
			{
				
				break;
			}
			case 10:
			{
				
				break;
			}
			
		}
	}
	
	
	//---------------------------------------------------------------------------------------------
	void ConsumeItem () 
	{
		// consume vegetables and water.
		if (m_homeTownStore.removeFromInventory(m_itemService.itemList[0]) == true)
		{
			hunger ++;
		}
		
		if (m_homeTownStore.removeFromInventory(m_itemService.itemList[1]) == true)
		{
			thirst ++;
		}
		
		
		if ((NPC_ClassID == 5) || (NPC_ClassID == 6))
		{
			if (m_homeTownStore.removeFromInventory(m_itemService.itemList[4]) == true)
			{
				hunger ++;
			}
		}
		
		if (NPC_ClassID > 6)
		{
			if (m_homeTownStore.removeFromInventory(m_itemService.itemList[4]) == true)
			{
				hunger ++;
			}
			if (m_homeTownStore.removeFromInventory(m_itemService.itemList[5]) == true)
			{
				hunger ++;
			}
			m_homeTownStore.removeFromInventory(m_itemService.itemList[12]);
		}
	}
	
	//---------------------------------------------------------------------------------------------
	
	void ProduceComplexItem()
	{
		
	
	
	
	}
	// take in X amount of consumable items. 
	// spit out x amount of tertiary items.
	
	
	//---------------------------------------------------------------------------------------------
	void HungerThirstUpdate()
	{
		hunger --;
		thirst --;
		energy --;
		if (hunger <= 0)
		{
			hunger = 0;
			health --;
		}
		if (thirst <= 0)
		{
			thirst = 0;
			health --;
		}
		if (energy <=0)
		{
			energy = 0;
			health --;
		}
		
		if (health <= 0)
		{
			Debug.Log (name + " Game Over " + this);
			Destroy(gameObject);
		}
		
	}
	
	
}
